﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Supports
{
    public class Week
    {
        private int _num;

        public int Num
        {
            get { return _num; }
            set { _num = value; }
        }
        private String _description;

        public String Description
        {
            get { return _description; }
            set { _description = value; }
        }
        private DateTime _beginDate;

        public DateTime BeginDate
        {
            get { return _beginDate; }
            set { _beginDate = value; }
        }

        private DateTime _endDate;

        public DateTime EndDate
        {
            get { return _endDate; }
            set { _endDate = value; }
        }

        public Week()
        {
        }

        public Week(int num, String description, DateTime beginDate, DateTime endDate)
        {
            this._num = num;
            this._description = description;
            this._beginDate = beginDate;
            this._endDate = endDate;
        }

        public static List<Week> ListWeeks(int year)
        {
            DateTime beginDate = new DateTime(year, 1, 1);
            DateTime endDate = beginDate.AddDays(6);

            List<Week> lWeek = new List<Week>();
            int num = 1;
            lWeek.Add(new Week(num, "Week " + num + ": " + beginDate.ToShortDateString() + " - " + endDate.ToShortDateString(), beginDate, endDate));

            while (endDate.Year == beginDate.Year)
            {
                num++;
                beginDate = endDate;
                endDate = beginDate.AddDays(6);
                lWeek.Add(new Week(num, "Week " + num + ": " + beginDate.ToShortDateString() + " - " + endDate.ToShortDateString(), beginDate, endDate));
            }
            return lWeek;
        }

        public static void GetWeek(int week, int year, ref DateTime beginDate, ref DateTime endDate)
        {
            int days = 0;

            beginDate = new DateTime(DateTime.Now.Year, 1, 1);

            switch (beginDate.DayOfWeek.ToString())
            {
                case "Sunday":
                    {
                        days = 7;
                        break;
                    }
                case "Monday":
                    {
                        days = 6;
                        break;
                    }
                case "Tuesday":
                    {
                        days = 5;
                        break;
                    }
                case "Wednesday":
                    {
                        days = 4;
                        break;
                    }
                case "Thursday":
                    {
                        days = 3;
                        break;
                    }
                case "Friday":
                    {
                        days = 2;
                        break;
                    }
                case "Saturday":
                    {
                        days = 1;
                        break;
                    }
            }

            endDate = beginDate.AddDays(days);

            for (int i = 1; i < week; i++)
                beginDate = beginDate.AddDays(6);

            if (week > 1)
                endDate = beginDate.AddDays(6);
        }
    }
}